<script lang="ts" setup name="XtxCity">
export type CityResult = {
  provinceCode: string;
  provinceName: string;
  cityCode: string;
  cityName: string;
  countyCode: string;
  countyName: string;
}
import axios from 'axios';
import { ref, watch } from 'vue'
import { onClickOutside } from '@vueuse/core'
// 接收父组件传过来的数据
defineProps<{
  userAddress: string
}>()
// 子传父
const emit = defineEmits<{
  (e: "changeCity", value: CityResult): void
}>()
const active = ref(false)
const ClickActive = () => {
  active.value = !active.value
}
// 点击弹层外部关闭弹层
const target = ref(null)
onClickOutside(target, () => {
  active.value = false
})
// 城市列表类型 TS
type AreaList = {
  code: string
  level: number
  name: string
  areaList: AreaList[]
}

// 获取城市数据
const CityList = ref<AreaList[]>([])
// 回复城市数据
const cacheList = ref<AreaList[]>([])
const getCityList = async () => {
  const res = await axios.get<AreaList[]>('https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json')
  CityList.value = res.data
  cacheList.value = res.data
}
getCityList()
// 选择城市数据储存
const changeResult = ref({
  provinceCode: '',
  provinceName: '',
  cityCode: '',
  cityName: '',
  countyCode: '',
  countyName: ''
})
// 点击切换地址数据
const selectCity = (item: AreaList) => {
  // 获取省数据
  if (item.level === 0) {
    changeResult.value.provinceCode = item.code
    changeResult.value.provinceName = item.name
    CityList.value = item.areaList
  }
  // 获取市数据
  if (item.level === 1) {
    changeResult.value.cityCode = item.code
    changeResult.value.cityName = item.name
    CityList.value = item.areaList
  }
  // 获取区县数据
  if (item.level === 2) {
    changeResult.value.countyCode = item.code
    changeResult.value.countyName = item.name
    active.value = false
    emit('changeCity', changeResult.value)
  }
}
// 关闭时恢复城市数据
watch(active, (value) => {
  if (!value) {
    CityList.value = cacheList.value
  }
})
</script>
<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="ClickActive" :class="{ active: active }">
      <span class="value" v-if="userAddress">{{
        userAddress
      }}</span>
      <span class="placeholder" v-else>请选择配送地址</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="active">
      <span class="ellipsis" v-for="item in CityList" :key="item.code" @click="selectCity(item)">{{ item.name }}</span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;

  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;

    &.active {
      background: #fff;
    }

    .placeholder {
      color: #999;
    }

    .value {
      color: #666;
      font-size: 12px;
    }

    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }

  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;

    >span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;

      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
